<?php

if(!defined('IN_SITE')) {
	exit('Access Denied');
}

class hschool_user{
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();

	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}

	/*
	* 取出用户信息
	*/
	public function info($user_id) {
		$info_array = array();
		if(is_array($user_id)){
			$info_sql = "SELECT * FROM ".DB::table('users')." WHERE user_id IN (".dimplode($user_id).")"; 
			$info_array = DB::fetch_all($info_sql);
		}else{
			$info_sql = "SELECT * FROM ".DB::table('users')." WHERE user_id='".$user_id."' LIMIT 1"; 
			$info_array = DB::fetch_first($info_sql);
		}
		return $info_array;
	}
	/*
	* 根据用户名 取出用户信息
	*/
	public function get_info_by_user_name($user_name) {
		$info_array = array();
		if(!empty($user_name)){
			$info_sql = "SELECT * FROM ".DB::table('users')." WHERE user_name='".$user_name."' LIMIT 1"; 
			$info_array = DB::fetch_first($info_sql);
		}
		return $info_array;
	}
	/*
	* 取出用户信息
	*/
	public function get_cm_type_name($cm_type) {
		$cm_type_name_array = array(
			'20' => '班主任',
			'21' => '学生',
			'22' => '任课老师',
			'23' => '家长'
		);
		return $cm_type_name_array[$cm_type];
	}

	/*
	* 用户密码修改
	*/
	public function user_change_dept($old_password,$new_password,$confirm_password) {
		global $_G;
		//检验当前密码为空
		$return = $this->return_json(false,'操作失败',0);
		if(empty($old_password)){
			return $this->return_json(true,'您输入的当前密码为空,请重新输入',0);
		}
		//检验两次密码是否一致
		if($new_password!=$confirm_password){
			return $this->return_json(true,'您输入的两次密码不匹配,请重新输入',0);
		}
		//检验新密码不能和目前密码一致
		if($new_password==$old_password){
			return $this->return_json(true,'新密码不能和当前密码一致,请重新输入',0);
		}
		//检验旧密码是否有效
		$sql = "SELECT user_id FROM ".DB::table('users')." WHERE user_id='".$_G['user_id']."' AND user_password='".encode_password($old_password)."' LIMIT 1";
		$result = DB::result_first($sql);
		if($result){
			//执行修改密码
			$data=array(
				'user_password'=>encode_password($new_password)
			);
			$where = array(
				'user_id'=>$_G['user_id']
			);
			$result = DB::update('users',$data,$where);
			if($result){
				return $this->return_json(false,'您的密码修改成功',0);
			}else{
				return $this->return_json(true,'您的密码没有变更',0);
			}
		}else{
			return $this->return_json(true,'您输入的当前密码不正确,请重新输入',0);
		}
	}

	/*
	*  前台用户信息修改 和 后端分离
	*/
	public function user_set_profile($user_profile) {
		global $_G;
		
		$data = array(
			'user_realname'=>$user_profile['user_realname'],
			'user_nickname'=>$user_profile['user_nickname'],
			'user_phone'=>$user_profile['user_phone'],
			'user_email'=>$user_profile['user_email'],			
			'student_no'=>$user_profile['student_no'],
			'address'=>$user_profile['address'],
			'gender'=>$user_profile['gender'],
			'about'=>$user_profile['about'],
			'qq'=>$user_profile['qq']
		);
		
		if($_G['member']['user_phone'] != $user_profile['user_phone'] )
		{
			$data['phone_validation']=0;
		}
		
		if($_G['member']['user_email'] != $user_profile['user_email'] )
		{
			$data['email_validation']=0;
			$data['email_token']='';
			$data['email_token_exptime']='';
		}
		if($user_profile['class_id']){
			$data['class_id']=$user_profile['class_id'];
			//判断班级是否有变更
			$sql = "SELECT class_id FROM ".DB::table('class_member')." WHERE user_id='".$_G['user_id']."' LIMIT 1";
			$class_id = DB::result_first($sql);
			if($user_profile['class_id'] != $class_id){
				//更新用户班级属性
				DB::update('class_member', array('class_id'=>$user_profile['class_id']), array('user_id'=>$_G['user_id']));
			}
		}
		if($user_profile['dept_id']){
			$data['dept_id']=$user_profile['dept_id'];
		}
		$where = array(
			'user_id'=>$_G['user_id']
		);
		$result = DB::update('users',$data,$where);
		if($result){
			return $this->return_json(false,'您的信息修改成功',0);
		}else{
			return $this->return_json(true,'您的信息没有变更',0);
		}
	}

	/*
	*  更新用户头像
	*/
	public function user_set_avatar($user_avatar,$user_id) {
		$data = array(
			'user_avatar'=>$user_avatar
		);
		$where = array(
			'user_id'=>$user_id
		);
		$result = DB::update('users',$data,$where);
		if($result){
			return $this->return_json(false,'操作成功',0);
		}else{
			return $this->return_json(true,'操作失败或信息为变化',0);
		}
	}

	/*
	* 取出用户所属部门与管理班级合集
	*/
	public function get_user_class_dept($user_id='') {
		//TODO 考虑到家长与自行注册的用户没有归属,暂未做分层设计 用户分层管理 考虑中
	}

	/*
	* ajax 返回信息
	*/
	public function return_json($error,$message,$output=1) {
		$return_array = array(
			"error" => $error,
			"message" => $message
		);
		if($output){
			echo json_ext($return_array);
			die;
		}else{
			return json_ext($return_array);
		}
	}
}

?>